<template>
	<view class="box">
		<!-- logo -->
		<view class="login_title">
			欢迎登录小U商城
		</view>
		<image class="login_logo"  src="../../static/logo/orange.png" alt="" />
		<!-- #ifdef MP-WEIXIN -->
		<button class="login_but" hover-class="touchOpacity" @click="wxLogin" @getphonenumber="wxLogin" open-type="getPhoneNumber">
			微信用户一键登录
		</button>
		<!-- #endif -->
		<view  class="login_but" hover-class="touchOpacity" @click="goPhone" >手机号验证注册/登录</view>
	</view>
</template>

<script>

	export default {
		data() {
			return {
			}
		},
		onLoad() {},
		methods: {
			goPhone(){
				uni.navigateTo({
					url: '/pages/textlogin/textlogin'
				})
			},
			
			// 微信一键登录
			async wxLogin(e){
				console.log(e);
				if(e.detail.errMsg != "getPhoneNumber:ok") return;
				let {code, encryptedData, iv} = e.detail;
				
				wx.login().then(async res => {
					console.log(res);
					
					let [err, {data}] = await uni.request({
						method: "POST",
						url: `${this.$baseApi}wxlogin`,
						data: {
							iv,
							encryptedData,
							code: res.code,
							AppID: 'wx072b7034819df56a',
							secret: '83b7c642bbf1e85e617d852f9d057ce4'
						}
					});
					
					console.log(data);
					
					uni.showToast({
						title: data.msg
					});
					uni.setStorageSync('userinfo', data.list);
					
					setTimeout(() => {
						uni.switchTab({
							url: `/pages/index/index`
						})
					}, 2000);
				})
			}
		}
	}
</script>

<style>
	/* 导入外部的样式文件 */
	@import url("@/static/css/login.css");
</style>
